package stc.skymobi.netty.handler.codec.http.transformer;

import io.netty.handler.codec.http.HttpRequest;
import stc.skymobi.util.Transformer;


/**
 * @author hp
 *
 */
public class RequestCodeGetter implements Transformer<HttpRequest, String> {

    /* (non-Javadoc)
     * @see com.skymobi.http.HttpRequestTransformer#transform(org.apache.asyncweb.common.HttpRequest)
     */
    public String transform(HttpRequest request) {
        String requestCode = request.getUri();

        requestCode = requestCode.trim();
        //部分省份会篡改http请求,将post的url前面加上host地址,需要清除掉 for eg: http://appid.fivesky.net:4009/UpdateProvision/...
        if(requestCode.startsWith("http://")){
            requestCode = requestCode.replace("http://","");
            int idx = requestCode.indexOf("/");
            requestCode = requestCode.substring(idx+1);
        }
        if (requestCode.startsWith("/")) {
            requestCode = requestCode.substring(1);
        }
        if ( requestCode.endsWith("/") ) {
            requestCode = requestCode.substring(0, requestCode.length() - 1);
        }
        
        //	for eg: UpdateProvision/app/1/city/610041
        int idx = requestCode.indexOf('/');
        if ( -1 != idx ) {
        	requestCode = requestCode.substring(0,idx);	// escape '/' character
        }
        
        //	for eg: UpdateProvision?param1=111&param2=222
        idx = requestCode.indexOf('?');
        if ( -1 != idx ) {
        	requestCode = requestCode.substring(0, idx);	// escape '?' character and more
        }
        return  requestCode;
    }

}
